﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

using Pg.BioMedics.SDR.Entities;
using Pg.BioMedics.SDR.Services;

namespace Pg.BioMedics.SDR.Web.Modules
{
    public class DownloadModule : IHttpModule
    {
        #region Constants
        
        public const string DownloadPath = "Download.ashx";

        #endregion

        #region Fields

        private static IDictionary<string, Document> downloadRequests = new Dictionary<string, Document>(); 

        #endregion

        #region Static methods

        public static Guid RegisterRequest(Document document)
        {
            lock (downloadRequests)
            {
                Guid uniqueId = Guid.NewGuid();
                downloadRequests.Add(uniqueId.ToString(), document);

                return uniqueId;
            }
        }

        private static Document FindRequest(string uniqueId)
        {
            lock (downloadRequests)
            {
                if (!downloadRequests.ContainsKey(uniqueId))
                    return null;

                Document document = downloadRequests[uniqueId];
                downloadRequests.Remove(uniqueId);

                return document;
            }
        }

        #endregion

        #region Event handlers

        private void OnPostAuthorizeRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;

            if (application.Request.AppRelativeCurrentExecutionFilePath.Contains(DownloadPath))
            {
                if (application.Request.QueryString.Count == 1)
                {
                    string uniqueId = application.Request.QueryString[0];
                    Document document = FindRequest(uniqueId);
                    if (document != null)
                    {
                        Content content = ServiceFactory.IndexingService.GetContent(document);
                        if (content != null)
                        {
                            HttpResponse response = application.Response;

                            response.Clear();
                            response.AddHeader("content-disposition", "attachment;filename=" + content.FileName);
                            response.OutputStream.Write(content.Data, 0, content.Data.Length);
                            response.End();
                            
                        }
                    }
                }
            }
        }

        #endregion

        #region IHttpModule Members        

        public void Init(HttpApplication context)
        {
            context.PostAuthorizeRequest += new EventHandler(OnPostAuthorizeRequest);           
        }

        public void Dispose()
        {
            downloadRequests.Clear();
            GC.Collect();
        }

        #endregion
    }
}
